﻿Report = function () {

}

Report.TitleColumns = function (_columns) {
    var myColumns = "<table class=\"rounded-corner\">";
    myColumns += "<thead><tr>"; //<th class=\"rounded-company\" scope=\"col\"></th>
    for (var i = 0; i < _columns.length; i++) {
        myColumns += "<th class=\"rounded\" scope=\"col\">" + _columns[i] + "</th>";
    }
    
    myColumns += "</tr></thead>";
    var n = _columns.length - 1;
    myColumns += "<tfoot><tr><td class=\"rounded-foot-left\" colspan=\"" + n + "\"><em> </em></td>";
    myColumns += "<td class=\"rounded-foot-right\">&nbsp;</td>";
    myColumns += "</tr></tfoot>";
    return myColumns;
}

Report.CreateTable = function (title, content) {
    var myTable = title + content + "</table>";
    $("#div_list").children().remove();
    $("#div_list").append(myTable);
}

Report.BaoCaoDoanhThu = function () {
    var serviceUrl = Constants.service + "ReportDoanhThu";
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service        
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var results = msg.ReportDoanhThuResult;
            var data = results;
            var columns = new Array("Mã BPBK", "Hàng Hóa", "Tên BPBK", "Từ Đơn Vị", "Đến Đơn Vị", "Khách Hàng", "Trọng Lượng", "Doanh Thu");
            var myDataColumns = "<tbody>";
            var sum = 0;
            for (var i = 0; i < data.length; i++) {
                var id = data[i].MaBill
                myDataColumns += "<tr >";
                myDataColumns += "<td>" + data[i].MaBill + "</td>"
                myDataColumns += "<td>" + data[i].TenLoaiBpbk + "</td>"
                myDataColumns += "<td>" + data[i].TenBpbk + "</td>"
                myDataColumns += "<td>" + data[i].TuDonVi + "</td>"
                myDataColumns += "<td>" + data[i].TenDonVi + "</td>"
                myDataColumns += "<td>" + data[i].TenKhachHang + "</td>"
                myDataColumns += "<td>" + data[i].TrongLuong + "</td>"
                myDataColumns += "<td>" + data[i].ThanhTien + "</td></tr>"
                sum += parseFloat(data[i].ThanhTien);
            }
            myDataColumns += "<tr><td colspan =\"7\">Tổng Doanh Thu : </td>"
            myDataColumns += "<td>" + sum + "</td></tr>"
            myDataColumns += "</tbody>";
            Report.CreateTable(Report.TitleColumns(columns), myDataColumns);
        },
        error: function (err) {
            // When Service call fails
        }
    });
}
